# coding=utf-8
from flask import  Flask
app=Flask(__name__)
import json
import tiES
from  tiResponse import *
from flask import request
from tidao import  Tidao;
from tiES import  TiES
from functools import wraps
from flask import make_response
tidao=Tidao();
ties=TiES()
from Pagination import  Pagination

def allow_cross_domain(fun):
    @wraps(fun)
    def wrapper_fun(*args, **kwargs):
        rst = make_response(fun(*args, **kwargs))
        rst.headers['Access-Control-Allow-Origin'] = '*'
        rst.headers['Access-Control-Allow-Methods'] = 'PUT,GET,POST,DELETE'
        allow_headers = "Referer,Accept,Origin,User-Agent"
        rst.headers['Access-Control-Allow-Headers'] = allow_headers
        return rst
    return wrapper_fun


@app.route('/')
def hello_workd():
    return '你好'

##http://localhost:5000/subjecttyperel

@app.route('/subjecttyperel',methods=['GET','POST'])
@allow_cross_domain
def getSubjectTypeRel():
    v=tidao.getSubjectTypeRel();
    return cobjson(v) ;
#http://localhost:5000/papertyperel
@app.route('/papertyperel',methods=['GET','POST'])
@allow_cross_domain
def getPaperRel():
    v=tidao.getPaperTypeRel();
    return cobjson(v) ;

#获取左侧推荐试卷列表
#http://localhost:5000/leftrecommend
@app.route('/leftrecommend',methods=['GET','POST'])
@allow_cross_domain
def getLeftRecommend():
    subject=request.args.get('subject')
    type=request.args.get('type')
    v=tidao.getLeftRecommend(subject,type);
    return json.dumps(v)

#获取单个试卷下面所有的试题
#http://localhost:5000/paperdetail?pid=8a914fb0-81d3-11e7-bc2b-00163e0070df
@app.route('/paperdetail',methods=['GET','POST'])
def getpaperdetail():
    pid=request.args.get('pid');
    v=tidao.getpaperdetail(pid)
    return cobjson(v)


#获取题目内容详情
#http://localhost:5000/tidetail?id=22331652-7f57-11e7-9aca-00163e0070df
@app.route('/tidetail',methods=['GET','POST'])
@allow_cross_domain
def getTiDetail():
    id=request.args.get('id')
    v=tidao.getTiDetail(id);
    return cobjson(v);


# 获取题目列表(分页)
#http://localhost:5000/tipage?pageno=1&pagesize=10
@app.route('/tipage',methods=['GET','POST'])
@allow_cross_domain
def getTisPage():
    keyword=request.form['paperyear']
    paperyear = request.form['paperyear']
    paperarea = request.form['paperarea']
    pageno=request.form['pageno']
    pagesize=request.form['pagesize']
    channel=request.form['channel']
    grade=request.form['grade']
    papertype=request.form['papertype']
    pageno=int(pageno)
    pagesize=int(20)
    v=tidao.getTisPage(keyword=keyword,channel=channel,grade=grade,paperyear=paperyear,paperarea=paperarea,papertype=papertype,pageno=pageno,pagesize=10);
    return cobobj(v);

#获取试卷列表(分页)
#http://localhost:5000/paperpage?pageno=1&pagesize=10
@app.route('/paperpage',methods=['GET','POST'])
@allow_cross_domain
def getPaperPage():
    keyword = request.form['paperyear']
    paperyear = request.form['paperyear']
    paperarea = request.form['paperarea']
    pageno = request.form['pageno']
    pagesize = request.form['pagesize']
    channel = request.form['channel']
    grade = request.form['grade']
    papertype = request.form['papertype']
    pageno = int(pageno)
    pagesize = int(20)
    v = tidao.getPaperPage(keyword=keyword, channel=channel, grade=grade, paperyear=paperyear, paperarea=paperarea,
                         papertype=papertype, pageno=pageno, pagesize=10);
    return cobobj(v);






#根据关键字搜索题目列表
#http://localhost:5000/searchti?pageno=1&pagesize=10&keyword=试题分析
@app.route('/searchti')
def searchTi():
    keyword = request.args.get('keyword')
    paperyear = request.args.get('paperyear')
    paperarea = request.args.get('paperarea')
    pageno = request.args.get('pageno')
    pagesize = request.args.get('pagesize')
    channel = request.args.get('channel')
    grade = request.args.get('grade')
    ptype=request.args.get('ptype');
    pageno = int(pageno)
    pagesize = int(pagesize)
    v = tidao.getTisPage(keyword=keyword, channel=channel, grade=grade, paperyear=paperyear, paperarea=paperarea,ptype=ptype,
                         pageno=1, pagesize=10);
    return cobjson(v)
#获取相关试题
#http://localhost:5000/simlartimus
@app.route('/simlartimus')
@allow_cross_domain
def simlarTimus():
    id = request.args.get('id')
    v=tidao.simlarTimus(id);
    return cobjson(v)
if __name__ == '__main__':
    app.run(debug=True,threaded=True)